<!-- 提货单模板 -->
<template>
  <div class="lading_bill_template">
    <SearchCard
        ref="searchCardRef"
        :searchTemplate="searchTemplate"
        :more="false"
        @add-event="addEvent"
        @reset="getList"
        @search="(data) => getList(data)" />
    <el-card class="table_card">
      <Table
        :data="tableData"
        v-loading="tableLoading"
        :height="height">
        <el-table-column
          prop="fullName"
          label="模板名称"
          :resizable="false" />
        <el-table-column
          label="已选配置"
          :resizable="false">
          <template #default="scope">
            <span
              v-for="item in JSON.parse(scope.row.templateConfig).title"
              :key="item"
              class="tag">
              <el-tag v-if="item === 'DELIVERY_DATE'">提货时间</el-tag>
              <el-tag v-if="item === 'PRODUCT_NAME'">品名</el-tag>
              <el-tag v-if="item === 'PACKAGING'">包装</el-tag>
              <el-tag v-if="item === 'SPECIFICATION'">规格</el-tag>
              <el-tag v-if="item === 'QUANTITY'">数量（袋）</el-tag>
              <el-tag v-if="item === 'WEIGHT'">重量（吨）</el-tag>
              <el-tag v-if="item === 'SUPPLIER'">供货单位</el-tag>
              <el-tag v-if="item === 'MANUFACTURER'">厂家</el-tag>
              <el-tag v-if="item === 'PAID_UNIT_PRICE'">已付单价（元）</el-tag>
              <el-tag v-if="item === 'PURCHASE_UNIT_PRICE'">采购单价（元）</el-tag>
              <el-tag v-if="item === 'WAREHOUSING_FEE'">仓储费</el-tag>
              <el-tag v-if="item === 'PURCHASE_TOTAL_PRICE'">采购总价（元）</el-tag>
              <el-tag v-if="item === 'DEDUCT_DEPOSIT'">已抵扣保证金</el-tag>
              <el-tag v-if="item === 'ACTUAL_PAYMENT'">实际应付款</el-tag>
              <el-tag v-if="item === 'DRIVER_INFO'">司机信息</el-tag>
              <el-tag v-if="item === 'TRIP'">车次</el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="启用状态"
          :resizable="false">
          <template #default="scope">
            <el-tag :type="scope.row.isEnable ? 'primary' : 'danger'">
              {{ scope.row.isEnable ? config.button.text.enable
                : config.button.text.stop }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="200"
          :resizable="false">
          <template #default="scope">
            <el-button
              type="warning"
              v-permission="validateButtonPermission(route, 1)"
              @click="editEvent(scope.row)">
              {{ config.button.text.edit }}
            </el-button>
            <el-button
              type="success"
              @click="previewEvent(scope.row)">
              {{ config.button.text.preview }}
            </el-button>
          </template>
        </el-table-column>
      </Table>
      <Pagination
        :pageNum="pageNum"
        :pageSize="pageSize"
        :total="total"
        @handle-size-change="(val) => pageSize = val"
        @handle-current-change="(val) => pageNum = val" />
    </el-card>

    <!-- 新增修改 -->
    <Edit
      ref="editRef"
      :rowData="rowData"
      @save="save" />
    <!-- 预览图片 -->
    <Preview
      ref="previewRef"
      :rowData="rowData" />
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import config from "@/common/element-plus/config";
import ApiDeliveryTemplate from "@/api/modules/delivery-template"
import message from "@/common/element-plus/message";
import { useTable } from "@/hooks/useTable"
import Pagination from "@/components/Pagination.vue"
import Table from "@/components/Table.vue"
import { validateButtonPermission } from "@/common/utils/validate";
import { useRoute } from "vue-router"
import Edit from "./component/Edit.vue"
import Preview from "./component/Preview.vue"
import { useAppStore } from "@/stores/app"
import { storeToRefs } from "pinia";
import app from "@/common/config/app";
import SearchCard from "@/components/SearchCard.vue"


const route = useRoute()
// 查询表单
const formData = ref({})
// 新增修改组件实例
const editRef = ref()
// 预览组件实例
const previewRef = ref()
// 表格每行数据
const rowData = ref({})
// 表格高度(固定表头)
const height = ref(0)
// 固定数据
const list = ref([]);
// 查询卡片实例
const searchCardRef = ref()

// 查询模板
const searchTemplate = ref([
  {
    label: "模板名称",
    type: "input",
    placeholder: "模板名称",
    value: "fullName",
    isClosed: false,
  },
  {
    label: "启用状态",
    type: "status",
    placeholder: "启用状态",
    value: "isEnable",
    isClosed: false,
  }
]);

// hooks
const { tableMethods, tableState, pagination } = useTable({
  fetchDataApi: async (data) => {
    const { pageSize, pageNum } = pagination
    const res = await ApiDeliveryTemplate.selectPage(data, {
      pageNum: pageNum.value,
      pageSize: pageSize.value,
    })
    return {
      list: res.data.records,
      total: res.data.total
    }
  }
})
const { getList } = tableMethods
const { pageSize, pageNum } = pagination
const { total, tableLoading, tableData } = tableState

// 打开新增对话框
const addEvent = () => {
  editRef.value.visibility = true
  rowData.value = {}
  rowData.value.list = list.value
}

// 修改
const editEvent = (row) => {
  editRef.value.visibility = true
  rowData.value = Object.assign({}, row)
  rowData.value.list = list.value
}

// 预览
const previewEvent = (row) => {
  previewRef.value.visibility = true
  rowData.value = {}
  rowData.value.id = row.id
}

// 重置
const reset = () => {
  formData.value = {}
  getList()
}

// 保存后回调
const save = async () => {
  editRef.value.visibility = false
  message.success()
  await getList()
}

// 监听查询卡片大小变化
const resizeObserver = new ResizeObserver(entries => {
  for (const entry of entries) {
    if (contentFullScreen.value) {
      height.value = (window.innerHeight - app.otherHeight - app.tagHeight - entry.contentRect.height) + "px"
    } else {
      height.value = (window.innerHeight - app.otherHeight - app.tagHeight - app.headerHeight - entry.contentRect.height) + "px"
    }
  }
})

const appStore = useAppStore()

// 保持响应式
const { contentFullScreen } = storeToRefs(appStore)

onMounted(async () => {
  const data = await ApiDeliveryTemplate.getTemplateTitle()
  list.value = data.data
  resizeObserver.observe(searchCardRef.value.$el)
})
</script>

<style lang="scss" scoped>
.lading_bill_template {
  .search_card {
    margin-bottom: 4px;

    :deep(.el-card__body) {
      padding: 10px;
      display: flex;
      justify-content: space-between;
    }

    .el-button {
      margin: 5px 10px 5px 0;
    }

    .query_button {
      margin-left: 100px;
    }

    .el-form-item {
      margin-right: 10px;
      padding: 5px 0;
    }
  }

  .table_card {
    :deep(.el-card__body) {
      padding: 0;
    }

    .tag {
      margin-right: 10px;
    }
  }
}
</style>
